﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using BLL;


public partial class Admin_EditVerifyCircuit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindDropDownList(this.drpDepartmetnName);
            bind();
        }
    }

    void bind()
    {
        int VerifyCircuitId = Utility.HttpHelp.getParamsInt("Id");
        //查询流程细节，绑定gridview
        BLL.VerifyCircuitManager dllVerifyCircuitManager = new BLL.VerifyCircuitManager();
        object[] _value = new object[1];
        _value[0] = VerifyCircuitId;
        DataTable dt = dllVerifyCircuitManager.get(BLL.VerifyCircuitManager.field.VerifyCircuitId.ToString(), _value);
        if (dt.Rows.Count > 0)
        {
            this.txtVerifyName.Text = dt.Rows[0]["VerifyCircuitName"].ToString();
            this.drpDepartmetnName.SelectedIndex = this.drpDepartmetnName.Items.IndexOf(this.drpDepartmetnName.Items.FindByText(dt.Rows[0]["DepartmentName"].ToString()));
        }
        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();

        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            HtmlInputControl hic = (HtmlInputControl)this.GridView1.Rows[i].FindControl("txtVerifyPeople");
            hic.Value = dt.Rows[i]["VerifyPeopleRealName"].ToString();
        }
    }
    protected void btnback_Click(object sender, EventArgs e)
    {
        Response.Redirect("VerifyCircuitManager.aspx");
    }

    /// <summary>
    /// 修改事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        DataTable _userTable = new DataTable();        
        int Id = 0;        
        string VerifyPeopleOld = "";
        BLL.UserInfoManager dllUserInfoManager = new BLL.UserInfoManager();
        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            Id = Convert.ToInt32(this.GridView1.DataKeys[i]["Id"]);
            VerifyPeopleOld = this.GridView1.DataKeys[i]["VerifyPeople"].ToString();
            HtmlInputText tb = (HtmlInputText)this.GridView1.Rows[i].FindControl("txtVerifyPeople");
            object[] _user = new object[1];
            _user[0] = tb.Value.Trim();
            _userTable = dllUserInfoManager.get(UserInfoManager.field.RealName.ToString(), _user);
            if (_userTable.Rows.Count == 0)
            {
                continue;
            }
            object[] _value = new object[4];
            _value[0] = Id;
            _value[1] = _userTable.Rows[0]["UserName"].ToString();
            _value[2] = VerifyPeopleOld;
            _value[3] = tb.Value.Trim();
            int val = new VerifyCircuitManager().edit(VerifyCircuitManager.field.Id.ToString(), _value);
            if (val == 0)
            {
                Utility.Error.RedirectErrorPage("修改审核流程未成功，请重试！");
            }

        }
        bind();
    }

    /// <summary>
    /// 绑定部门bindDropDownList
    /// </summary>
    void bindDropDownList(DropDownList _dropDownList)
    {
        DepartmentInfoManager dllDepartment = new DepartmentInfoManager();
        if (_dropDownList.Equals(this.drpDepartmetnName))
        {
            _dropDownList.DataSource = dllDepartment.getNoTree();
            _dropDownList.DataTextField = "DepartmentName";
            _dropDownList.DataValueField = "DepartmentId";
            _dropDownList.DataBind();
            if (_dropDownList.Items.Count == 0)
            {
                ListItem li = new ListItem("无部门");
                _dropDownList.Items.Add(li);
            }
        }
    }
}